<?php
/**
 * 工具类，处理字符串类
 * Created by PhpStorm.
 * User: wzy
 * Date: 2017/6/14
 * Time: 13:51
 */
namespace soa\tools;

use soa\client;
use artisan\cache;

class HandleStr extends client
{
    private $supType = ['idcard','mobile','name','address'];
    /**
     * 隐藏字符串
     * @param $string
     * @param $type
     * @return string
     */
    public function hideStr($string,$type){
        if(empty($type) || !in_array($type,$this->supType)){return false;}
        if(empty($string)){return false;}
        $startEnd = $this->getStartEnd($type);  //根据类型获取开始于截止位数
        $str = mb_substr($string,$startEnd['start']?:0,$startEnd['end'] == 'end'?null:$startEnd['end'],'UTF-8');   //需要替换字符串
        $hide = str_repeat('*',strlen($str));   //需要替换字符串长度
        $hideStr = str_replace($str,$hide,$string);
        if(empty($hideStr)){return false;}
        return $hideStr;
    }

    /**
     * 根据类型获取开始于截止位数
     * @param $type
     * @return array
     */
    public function getStartEnd($type)
    {
        $result = [];
        switch ($type){
            case 'idcard':
                $result = ['start'=>4,'end'=>-4];
                break;
            case 'mobile':
                $result = ['start'=>3,'end'=>-4];
                break;
            case 'name':
                $result = ['start'=>0,'end'=>-1];
                break;
            case 'address':
                $result = ['start'=>5,'end'=>'end'];   //utf-8一个字符3个字节
                break;

            default:
                break;
        }
        return $result;
    }



}